//
//  UserAccout.swift
//  WeiBo
//
//  Created by Tony on 2018/1/8.
//  Copyright © 2018年 Tony. All rights reserved.
//

import UIKit

class UserAccout: NSObject,NSCoding {
/*
    "access_token" = "2.00Ah4f_EPcTAODab492e96570cFW9P";
    "expires_in" = 157679999;
    isRealName = true;
    "remind_in" = 157679999;
    uid = 3955144238;
*/
    
    var access_token : String?
    var expires_in : TimeInterval = 0 {
        didSet {
            expires_date = Date.init(timeIntervalSinceNow: expires_in)
        }
    }
    // 用户Id
    var uid : String?
    
    // 昵称
    var screen_name : String?
    
    // 头像
    var avatar_large : String?
    
    // 过期时间
    var expires_date : Date?
    
    // 重写description，方便打印输出
    override var description: String {
        return dictionaryWithValues(forKeys: ["access_token","expires_in","uid","expires_date","avatar_large","screen_name"]).description
    }
    
    // init
    override init() {
        super.init()
    }
    
    init(dict: [String : AnyObject]) {
        super.init()
        
        // KVC
        setValuesForKeys(dict)
    }
    
    override func setValue(_ value: Any?, forUndefinedKey key: String) {
        
    }
    
    // 归档
    func encode(with aCoder: NSCoder) {
       
        aCoder.encode(access_token, forKey: "access_token")
        aCoder.encode(uid, forKey: "uid")
        aCoder.encode(expires_date, forKey: "expires_date")
        aCoder.encode(avatar_large, forKey: "avatar_large")
        aCoder.encode(screen_name, forKey: "screen_name")
        
    }
    // 解档
    required init?(coder aDecoder: NSCoder) {
        
        access_token = aDecoder.decodeObject(forKey: "access_token") as? String
        uid = aDecoder.decodeObject(forKey: "uid") as? String
        expires_date = aDecoder.decodeObject(forKey: "expires_date") as? Date
        avatar_large = aDecoder.decodeObject(forKey: "avatar_large") as? String
        screen_name = aDecoder.decodeObject(forKey: "screen_name") as? String
        
    }
    
    
}
